package com.flh.crm.workbench.service.Impl;

import com.flh.crm.utils.DateTimeUtil;
import com.flh.crm.utils.SqlSessionUtil;
import com.flh.crm.utils.UUIDUtil;
import com.flh.crm.workbench.dao.CustomerDao;
import com.flh.crm.workbench.dao.TranDao;
import com.flh.crm.workbench.dao.TranHistoryDao;
import com.flh.crm.workbench.domain.Customer;
import com.flh.crm.workbench.domain.Tran;
import com.flh.crm.workbench.domain.TranHistory;
import com.flh.crm.workbench.service.TranService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author FLH
 * @Date 2020/8/12 10:33
 * @Version 1.0
 */
public class TranServiceImpl implements TranService {

    private CustomerDao customerDao = SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);
    private TranDao tranDao = SqlSessionUtil.getSqlSession().getMapper(TranDao.class);
    private TranHistoryDao tranHistoryDao = SqlSessionUtil.getSqlSession().getMapper(TranHistoryDao.class);




    @Override
    public boolean save(Tran t, String customerName) {

        /*
        执行添加操作
        t中少了一项信息

        1.判断customerName，根据客户名称在客户表进行精确查询
        有客户id 封装到t
        没有新创建
        2.执行添加操作
        3.增加交易历史

         */

        boolean flag =true;

        Customer cus = customerDao.getCustomerByName(customerName);

        if (cus==null){
            cus = new Customer();
            cus.setId(UUIDUtil.getUUID());
            cus.setName(customerName);
            cus.setCreateBy(t.getCreateBy());
            cus.setCreateTime(DateTimeUtil.getSysTime());
            cus.setContactSummary(t.getContactSummary());
            cus.setNextContactTime(t.getNextContactTime());
            cus.setOwner(t.getOwner());
        int count1 = customerDao.save(cus);
        if (count1!=1){
            flag=false;

        }

        }

        //通过以上对于客户的处理,客户已经存在

        t.setCustomerId(cus.getId());

        int count2 = tranDao.save(t);

        if (count2!=1){

            flag=false;
        }

        //添加交易历史

        TranHistory th = new TranHistory();

        th.setId(UUIDUtil.getUUID());
        th.setTranId(t.getId());
        th.setStage(t.getStage());
        th.setMoney(t.getMoney());
        th.setExpectedDate(t.getExpectedDate());
        th.setCreateTime(DateTimeUtil.getSysTime());
        th.setCreateBy(t.getCreateBy());

        int count3 = tranHistoryDao.save(th);

        if (count3!=1){
            flag=false;

        }



        return flag;
    }

    @Override
    public Tran detail(String id) {



         Tran t = tranDao.detail(id);


        return t;
    }

    @Override
    public List<TranHistory> getHistoryListByTranId(String tranId) {

        List<TranHistory> thList = tranHistoryDao.getHistoryListByTranId(tranId);

        return thList;
    }

    @Override
    public boolean changeStage(Tran t) {


        boolean flag = true;

        //改变阶段
        int count1 = tranDao.changeStage(t);

        if (count1!=1){

            flag=false;
        }

        //交易阶段改变后生成交易历史
        TranHistory th = new TranHistory();

        th.setId(UUIDUtil.getUUID());
        th.setTranId(t.getId());
        //th.setStage(t.getStage());
        th.setMoney(t.getMoney());
        th.setExpectedDate(t.getExpectedDate());
        th.setCreateTime(DateTimeUtil.getSysTime());
        th.setCreateBy(t.getCreateBy());

        int count2 = tranHistoryDao.save(th);

        if (count2!=1){
            flag=false;

        }


        return flag;
    }

    @Override
    public Map<String, Object> getCharts() {

        //取得total

        int total = tranDao.getTotal();

        // 取得dataList

        List<Map<String,Object>> dataList = tranDao.getCharts();

        //返回map
        Map<String,Object> map = new HashMap<String, Object>();

        map.put("total",total);
        map.put("dataList",dataList);

        return map;
    }
}



















